<template>
	<view>
		<view style="padding:20rpx;font-size:1rem;margin-top:50rpx">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1" labelWidth="80">
				<u-form-item label="活动类型" prop="ptype" borderBottom ref="item1">
					<radio-group
					  @change="onChange" 
					>   
						<label v-for="(item, index) in radiolist1" :key="item.value" style="padding-right:20rpx">
							<radio :value="item.value" :checked="index === current" color="#F66B71"/>
							<text>{{item.name}}</text>
						</label>
					</radio-group>
				</u-form-item>
				<u-form-item label="条件" prop="pcondition" borderBottom ref="item1">
					<u--input v-model="model1.pcondition" border="none" :placeholder="`请输入${itemname}`">
					</u--input>
				</u-form-item>
				<u-form-item label="减额" prop="remoney" borderBottom ref="item1">
					<u--input v-model="model1.remoney" border="none" placeholder="请输入减额"></u--input>
				</u-form-item>
				<u-form-item label="开始时间" prop="starttime" borderBottom ref="item1" @click="onDisplay">
					<u--input v-model="model1.starttime" border="none" disabled placeholder="请选择时间"></u--input>
				</u-form-item>
				<u-form-item label="结束时间" prop="endtime" borderBottom ref="item1" @click="onDisplay2">
					<u--input v-model="model1.endtime" border="none" disabled placeholder="请选择时间"></u--input>
				</u-form-item>
			</u--form>

			<view>
				<u-button @click="editStoreInfo" class="editbtn" color="#F66B71" size="large" round>保存</u-button>
			</view>
		</view>

		<u-datetime-picker :show="timeshow" v-model="model1.starttime" mode="date" closeOnClickOverlay @confirm="onConfirm"
			@cancel="onClose"></u-datetime-picker>

		<u-datetime-picker :show="timeshow2" v-model="model1.endtime" mode="date" closeOnClickOverlay @confirm="onConfirm2"
			@cancel="onClose2"></u-datetime-picker>
	</view>
</template>

<script>
	import Config from '@/util/index.js'
	export default {
		data() {
			return {
				pcode: '',
				radio: "0",
				date: '',
				rcode: '',
				timeshow: false,
				timeshow2: false,
				taskStartTime: '',
				password: '',
				type: '',
				pcondition: '',
				remoney: '',
				starttime: '',
				endtime: "",
				isflag: '0',
				model1: {},
				current:0,
				itemname: '单笔金额',
				remoneyname: ['单笔金额', '积分', '订单数量'],
				pconditioname: '',
				activeDetail: {},
				radiolist1: [{
					value: "0",
					name:'满减'
				  },
					{
					  value: "1",
					  name:'积分减',
					  checked: true
					}
				],
				rules: {
					'pcondition': {
						type: 'string',
						required: true,
						message: '请填写条件',
						trigger: ['blur', 'change']
					},
					'remoney': {
						type: 'string',
						required: true,
						message: '请填写减额',
						trigger: ['blur', 'change']
					},
					'starttime': {
						type: 'string',
						required: true,
						message: '请选择开始时间',
						trigger: ['blur', 'change']
					},
					'endtime': {
						type: 'string',
						required: true,
						message: '请选择结束时间',
						trigger: ['blur', 'change']
					}
				}
			}
		},
		onLoad(options) {
			this.rcode = options.rcode
			if (options.actifo) {
				this.model1 = JSON.parse(options.actifo)
				this.current = this.model1.ptype
				if(this.current==0){
					this.itemname = "金额"
				}else{
					this.itemname = "积分"
				}
				
			}
		},
		methods: {
			onDisplay() {
			    this.timeshow=true;
			},
			onDisplay2() {
			    this.timeshow2=true;
			},
			addActive() {
				let params = {
					promotion: {
						pcode: this.pcode, //主键 
						restaurant: {
							rcode: this.rcode
						},
						management: {
							mid: uni.getStorageSync('loginInfo').mid
						}, //mid关联商户表
						ptype: this.current, //  类型
						pcondition: this.pcondition, //  回复内容
						remoney: this.remoney, //  回复内容
						starttime: this.starttime, //  回复内容
						endtime: this.endtime, //  回复内容
						isflag: this.isflag, //标识 默认0
					}
				}
				if (this.model1.pcondition == "") {
					uni.showToast({
						title: "单笔金额不能为空",
						icon: 'none',
						duration: 2000
					});
					return false
				}
				if (this.model1.remoney == "") {
					uni.showToast({
						title: "减少金额不能为空",
						icon: 'none',
						duration: 2000
					});
					return false
				}
				if (this.model1.starttime == "") {
					uni.showToast({
						title: "开始不能为空",
						icon: 'none',
						duration: 2000
					});
					return false
				}
				if (this.model1.endtime == "") {
					uni.showToast({
						title: "结束时间不能为空",
						icon: 'none',
						duration: 2000
					});
					return false
				}
				let _this = this
				Api.activeAdd(params).then(res => {
					if (res.retCode == 0) {
						if (_this.pcode) {
							uni.showToast({
								title: "修改成功",
								icon: 'none',
								duration: 2000
							});
						} else {
							uni.showToast({
								title: "添加成功",
								icon: 'none',
								duration: 2000
							});
						}
						uni.redirectTo({
							url: 'active?rcode=' + _this.rcode,
						})
					} else {
						uni.showToast({
							title: res.info,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			onClose() {
				this.timeshow = false;
			},
			onClose2() {
				this.timeshow2 = false;
			},
			formatDate(date) {
				let taskStartTime
				if (date.getMonth() < 9) {
					taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
				} else {
					taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
				}
				if (date.getDate() < 10) {
					taskStartTime += "0" + date.getDate()
				} else {
					taskStartTime += date.getDate()
				}
				this.setData({
					taskStartTime: taskStartTime,
				})
				return taskStartTime;
			},
			onConfirm(event) {
				this.timeshow = false
				this.starttime = this.formatDate(event)
				console.log("this.starttime",this.starttime)
			},
			onConfirm2(event) {
				this.timeshow2 = false
				this.endtime = this.formatDate(event)
			},
			onChange(event) {
				// const { picker, value, index } = event.detail;
				for (let i = 0; i < this.radiolist1.length; i++) {
					if (this.radiolist[i].value === evt.target.value) {
						this.current = i;
						this.itemname = this.remoneyname[i]
						break;
					}
				}
			}
		}
	}
</script>

<style scoped>
	.dplogo {
		width: 100rpx;
		height: 100rpx;
		display: block;
		margin: 20rpx auto;
		padding-top: 20rpx
	}

	.editbtn {
		width: 96%;
		height: 60rpx;
		position: fixed;
		bottom: 50rpx;
		left: auto;
		right: auto
	}

	.custom-class {
		height: 50rpx !important
	}
</style>
